package com.cssy.yd.logic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.logic.dao.LogicSoCancelDAO;
import com.cssy.yd.logic.dao.LogicSoCancelItemDAO;
import com.cssy.yd.logic.dao.LogicSoDAO;
import com.cssy.yd.logic.dao.LogicSoItemDAO;
import com.cssy.yd.logic.dao.LogicSoReturnDAO;
import com.cssy.yd.logic.dao.LogicSoReturnItemDAO;
import com.cssy.yd.logic.dao.LogicSoWareDAO;
import com.cssy.yd.logic.dao.LogicSoWareItemDAO;
import com.cssy.yd.logic.entity.LogicSoCancelDO;
import com.cssy.yd.logic.entity.LogicSoCancelItemDO;
import com.cssy.yd.logic.entity.LogicSoDO;
import com.cssy.yd.logic.entity.LogicSoItemDO;
import com.cssy.yd.logic.entity.LogicSoReturnDO;
import com.cssy.yd.logic.entity.LogicSoReturnItemDO;
import com.cssy.yd.logic.entity.LogicSoWareDO;
import com.cssy.yd.logic.entity.LogicSoWareItemDO;
import com.cssy.yd.logic.entity.LogicWhAffDO;
import com.cssy.yd.logic.service.ILogicSoCancelService;
import com.cssy.yd.logic.service.ILogicSoReturnService;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchCondition;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.exception.SyCodeException;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicSoCancelService")
public class LogicCancelServiceImpl implements ILogicSoCancelService{
	@Resource(name = "logicSoCancelDAO")
	private LogicSoCancelDAO logicSoCancelDAO;
	
	@Resource(name = "logicSoCancelItemDAO")
	private LogicSoCancelItemDAO logicSoCancelItemDAO;
	
	@Resource(name = "logicSoDAO")
	private LogicSoDAO logicSoDAO;
	
	@Resource(name = "logicSoItemDAO")
	private LogicSoItemDAO logicSoItemDAO;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name = "logicWhAffStockService")
	private ILogicWhAffStockService logicWhAffStockService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicSoCancelDO>  list=logicSoCancelDAO.find(scs);
		rs.setRecordCount(logicSoCancelDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		Integer i =logicSoCancelDAO.deleteLogicSoCancelDOByPrimaryKey(id);
		return null;
	}

	@Override
	public Integer update(LogicSoCancelDO t, User user) throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicSoCancelDAO.updateLogicSoCancelDO(t);
	}

	@Override
	public LogicSoCancelDO save(LogicSoCancelDO t, User user) throws SyBaseException {
		t.setDcruser(user.getName());
		t.setDctime(new Date());
		t.setId(IDCodeTypeUtil.getId());
		logicSoCancelDAO.insertLogicSoCancelDO(t);
		return t;
	}

	@Override
	public LogicSoCancelDO findListById(String id) throws SyBaseException {
		return logicSoCancelDAO.findLogicSoCancelDOByPrimaryKey(id);
	}

	@Override
	public void soSave(HttpServletRequest request, LogicSoCancelDO curAP)
			throws SyBaseException {
		try {
			String itemdata = request.getParameter("itemdata");
			String submitFlag = request.getParameter("submitFlag");
			curAP.setBillstatus(Short.parseShort(submitFlag));
			String jhrq =request.getParameter("jhrq");
			curAP.setJhrq(DateUtils.stringToDate(jhrq));
			List<LogicSoCancelItemDO> axxlist=JSONArray.parseArray(itemdata, LogicSoCancelItemDO.class);
			if (curAP.getId() != null && !"".equals(curAP.getId())) {// 修改
				curAP.setDuuser(SessionUtil.getUser(request).getName());
				curAP.setDutime(new Date());
				int rs=logicSoCancelDAO.updateLogicSoCancelDO(curAP);
				if(rs==1){
					if("0".equals(submitFlag)||"1".equals(submitFlag)){
						logicSoCancelItemDAO.deleteLogicSoCancelItemDO(curAP.getId());
						for(int i=0;i<axxlist.size();i++){
							axxlist.get(i).setId(IDCodeTypeUtil.getId());
							axxlist.get(i).setSoid(curAP.getId());
							logicSoCancelItemDAO.insertLogicSoCancelItemDO(axxlist.get(i));
						}
					}
					if("2".equals(submitFlag)){
						SearchConditions scs = new SearchConditions();
						scs.addCondition("socode",curAP.getOricode(),SearchCondition.OP_EQUAL);
						List<LogicSoDO> lpwlist=logicSoDAO.find(scs);
						lpwlist.get(0).setBillstatus(Integer.parseInt("5"));//仓库改单
						lpwlist.get(0).setDeliveryDate(curAP.getJhrq());
						logicSoDAO.updateLogicSoDO(lpwlist.get(0));
						List<LogicSoItemDO> soitem=logicSoItemDAO.findItemListBysoId(lpwlist.get(0).getId());
						for(int i=0;i<axxlist.size();i++){
							for(int k=0;k<soitem.size();k++){
								//TODO 待完善！hxm
//								if(soitem.get(k).getCode().equals(axxlist.get(i).getCode())&&soitem.get(k).getName().equals(axxlist.get(i).getName())){
//									soitem.get(k).setYgxquantity((soitem.get(k).getYgxquantity()==null?0.0:soitem.get(k).getYgxquantity())+axxlist.get(i).getGxquantity());
//									logicSoItemDAO.updateLogicSoItemDO(soitem.get(k));
//								}
							}
							axxlist.get(i).setYgxquantity((axxlist.get(i).getYgxquantity()==null?0.0:axxlist.get(i).getYgxquantity())+axxlist.get(i).getGxquantity());
							logicSoCancelItemDAO.updateLogicSoCancelItemDO(axxlist.get(i));
						}
						if(axxlist.size()>0){
							for(int i=0;i<axxlist.size();i++){
								LogicWhAffDO lwhaff=new LogicWhAffDO();
								lwhaff.setPrice(axxlist.get(i).getPrice());
								lwhaff.setBillCode(curAP.getSocode());
								lwhaff.setBillType("GD");
								lwhaff.setWhCode(curAP.getWhcode());
								lwhaff.setUnit(axxlist.get(i).getUnit());
								lwhaff.setWhCode(curAP.getWhcode());
								lwhaff.setHubName(curAP.getWhname());
								lwhaff.setStatus("1");
								lwhaff.setCode(axxlist.get(i).getCode());
								lwhaff.setName(axxlist.get(i).getName());
								lwhaff.setSpec(axxlist.get(i).getSpec());
								lwhaff.setBrand(axxlist.get(i).getBrand());
								lwhaff.setQuantity(axxlist.get(i).getGxquantity());
								logicWhAffStockService.updateWhAffstockQuantity(lwhaff, SessionUtil.getUser(request),false);
							}
						}
						}
					}else{
						throw new SyBaseException("当前行项目的数据已发生改变，请重新打开查看");
					}
				}else {// 新增
				String id=IDCodeTypeUtil.getId();
				curAP.setSocode(syCodeService.getCode("GD","GD",SyCode.DATE_YYMM,5));
				curAP.setId(id);
				curAP.setDcruser(SessionUtil.getUser(request).getName());
				curAP.setDctime(new Date());
				logicSoCancelDAO.insertLogicSoCancelDO(curAP);
				for(int i=0;i<axxlist.size();i++){
					axxlist.get(i).setSoid(id);
					axxlist.get(i).setId(IDCodeTypeUtil.getId());
					//TODO 待完善！hxm
//					axxlist.get(i).setYgxquantity((axxlist.get(i).getYgxquantity()==null?0.0:axxlist.get(i).getYgxquantity())+axxlist.get(i).getGxquantity());
					logicSoCancelItemDAO.insertLogicSoCancelItemDO(axxlist.get(i));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		
	}
	
	
	@Override
	public void xsSave(HttpServletRequest request, LogicSoCancelDO curAP)
			throws SyBaseException {
		try {
			String itemdata = request.getParameter("itemdata");
			String submitFlag = request.getParameter("submitFlag");
			curAP.setBillstatus(Short.parseShort(submitFlag));
			String jhrq =request.getParameter("jhrq");
			curAP.setJhrq(DateUtils.stringToDate(jhrq));
			List<LogicSoCancelItemDO> axxlist=JSONArray.parseArray(itemdata, LogicSoCancelItemDO.class);
			if (curAP.getId() != null && !"".equals(curAP.getId())) {// 修改
				curAP.setDuuser(SessionUtil.getUser(request).getName());
				curAP.setDutime(new Date());
				int rs=logicSoCancelDAO.updateLogicSoCancelDO(curAP);
				if(rs==1){
					if("3".equals(submitFlag)||"4".equals(submitFlag)){
						logicSoCancelItemDAO.deleteLogicSoCancelItemDO(curAP.getId());
						for(int i=0;i<axxlist.size();i++){
							axxlist.get(i).setId(IDCodeTypeUtil.getId());
							axxlist.get(i).setSoid(curAP.getId());
							logicSoCancelItemDAO.insertLogicSoCancelItemDO(axxlist.get(i));
						}
					}
					if("5".equals(submitFlag)){
						SearchConditions scs = new SearchConditions();
						scs.addCondition("socode",curAP.getOricode(),SearchCondition.OP_EQUAL);
						List<LogicSoDO> lpwlist=logicSoDAO.find(scs);
						lpwlist.get(0).setBillstatus(Integer.parseInt("6"));//销售改单
						lpwlist.get(0).setDeliveryDate(curAP.getJhrq());
						logicSoDAO.updateLogicSoDO(lpwlist.get(0));
						List<LogicSoItemDO> soitem=logicSoItemDAO.findItemListBysoId(lpwlist.get(0).getId());
						for(int i=0;i<axxlist.size();i++){
							//TODO 待完善！hxm
//							for(int k=0;k<soitem.size();k++){
//								if(soitem.get(k).getCode().equals(axxlist.get(i).getCode())&&soitem.get(k).getName().equals(axxlist.get(i).getName())){
//									soitem.get(k).setQuantity((axxlist.get(i).getGxquantity()==null?0.0:axxlist.get(i).getGxquantity()));
//									soitem.get(k).setYgxquantity((soitem.get(k).getYgxquantity()==null?0.0:soitem.get(k).getYgxquantity()));
//									logicSoItemDAO.updateLogicSoItemDO(soitem.get(k));
//								}
//							}
							axxlist.get(i).setYgxquantity((axxlist.get(i).getYgxquantity()==null?0.0:axxlist.get(i).getYgxquantity())+axxlist.get(i).getGxquantity());
							logicSoCancelItemDAO.updateLogicSoCancelItemDO(axxlist.get(i));
						}
						}
					}else{
						throw new SyBaseException("当前行项目的数据已发生改变，请重新打开查看");
					}
				}else {// 新增
				String id=IDCodeTypeUtil.getId();
				curAP.setSocode(syCodeService.getCode("GD","GD",SyCode.DATE_YYMM,5));
				curAP.setId(id);
				curAP.setDcruser(SessionUtil.getUser(request).getName());
				curAP.setDctime(new Date());
				logicSoCancelDAO.insertLogicSoCancelDO(curAP);
				for(int i=0;i<axxlist.size();i++){
					axxlist.get(i).setSoid(id);
					axxlist.get(i).setId(IDCodeTypeUtil.getId());
					logicSoCancelItemDAO.insertLogicSoCancelItemDO(axxlist.get(i));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

	
}
